﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Data;

namespace SaturniLibrary.CodeGenerate
{
    public class StringHelper
    {
        public static string SchemaNameToMetaName(string schemaName)
        {
            return SchemaNameToMetaName(schemaName, string.Empty, new char[] { '_' });
        }

        public static string SchemaNameToMetaName(string schemaName, string prefix)
        {
            return SchemaNameToMetaName(schemaName, prefix, new char[] { '_' });
        }

        /// <summary>
        /// 将数据库中的命名格式转换成类中的命名格式
        /// </summary>
        /// <param name="tableName">数据库命名格式(名字 或者 前缀_名字 或者 单词1_单词2... 或者 前缀_单词1_单词2...)</param>
        /// <param name="prefix">前缀, 默认是string.Empty</param>
        /// <param name="spliters">分隔符,默认"_"</param>
        /// <returns></returns>
        public static string SchemaNameToMetaName(string schemaName, string prefix, char[] spliters)
        {
            if (string.IsNullOrWhiteSpace(schemaName))
            {
                return string.Empty;
            }

            schemaName = schemaName.Trim();
            if (!string.IsNullOrWhiteSpace(prefix))
            {
                prefix = prefix.Trim();
                int posOfPrifex = schemaName.IndexOf(prefix);
                if (posOfPrifex >= 0)
                {
                    schemaName = schemaName.Substring(posOfPrifex + prefix.Length + 1);
                }
            }
            string[] words = schemaName.Split(spliters);
            TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
            for (int i = 0; i < words.Length; i++)
            {
                words[i] = textInfo.ToTitleCase(words[i]);
            }
            return string.Join("", words);
        }

        public static string DBTypeToCSharpMetaType(DbType dbType)
        {
            switch (dbType)
            {
                case DbType.AnsiString:
                    return "string";
                case DbType.Boolean:
                    return "bool";
                case DbType.Currency:
                case DbType.Decimal:
                case DbType.Double:
                    return "decimal";
                case DbType.Date:
                case DbType.Time:
                case DbType.DateTime:
                    return "DateTime";
                case DbType.Guid:
                    return "Guid";
                case DbType.Int16:
                case DbType.Int32:
                case DbType.Int64:
                    return "int";
                case DbType.Single:
                    return "float";
                case DbType.UInt16:
                case DbType.UInt32:
                case DbType.UInt64:
                    return "uint";
                default:
                    return "string";
            }
        }
    }
}
